package com.zhangye.test.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangye.test.model.Admin;
import com.zhangye.test.model.EasyAr;
import com.zhangye.test.model.Infomation;
import com.zhangye.test.service.EasyArService;
import com.zhangye.test.service.InfomationService;
import com.zhangye.test.service.SafeSessionService;
import com.zhangye.test.util.PageInfo;

/**
 * 点赞，分享信息表
 * 作者： 张叶
 * 创建时间：2018年6月22日  上午11:19:14
 */
@Controller
@RequestMapping("api/infomation")
public class InfomationApi {

	@Autowired
	private InfomationService infomationService;
	@Autowired
	private SafeSessionService safeSessionService;
	@Autowired
	private EasyArService easyArService;

	
	/**
	 * 我分享的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyShareNew")
	public ApiResult getMyShareNew(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = "id in (select easy_ar_id from infomation where admin_id = " + admin.getId() + " and type = 'share')";
			where += " and  status = 'on'";
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			String order = " order by create_at desc";
			List<Map<String, Object>> list = easyArService.selectByPageCategory(pageInfo, where, order,admin);
			if(list.size() > 0){
				for(int i = 0 ; i < list.size() ; i ++){
					String imageUrl = list.get(i).get("imageUrl").toString();
					imageUrl += "?imageView/2/w/320";
					list.get(i).put("thumUrl", imageUrl);
				}
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无分享作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}

	
	/**
	 * 我制作的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyMakeNew")
	public ApiResult getMyMakeNew(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = "admin_id = " + admin.getId();
			where += " and  status = 'on'";
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			String order = " order by create_at desc";
			List<Map<String, Object>> list = easyArService.selectByPageCategory(pageInfo, where, order,admin);
			if(list.size() > 0){
				for(int i = 0 ; i < list.size() ; i ++){
					String imageUrl = list.get(i).get("imageUrl").toString();
					imageUrl += "?imageView/2/w/320";
					list.get(i).put("thumUrl", imageUrl);
				}
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无制作作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}

	
	/**
	 * 我赞过的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyGiveNew")
	public ApiResult getMyGiveNew(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = "id in (select easy_ar_id from infomation where admin_id = " + admin.getId() + " and type = 'give')";
			PageInfo pageInfo = new PageInfo();
			if (pageInfo.getPage() < 2
					|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
				pageInfo.setCount(0);
			}
			String order = " order by create_at desc";
			List<Map<String, Object>> list = easyArService.selectByPageCategory(pageInfo, where, order,admin);
			if(list.size() > 0){
				for(int i = 0 ; i < list.size() ; i ++){
					String imageUrl = list.get(i).get("imageUrl").toString();
					imageUrl += "?imageView/2/w/320";
					list.get(i).put("thumUrl", imageUrl);
				}
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无点赞作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	/**
	 * 添加
	 * type:share  give  uploadImage   uploadVideo
	 * 作者： 张叶
	 * 创建时间：2018年6月22日  上午11:21:22
	 */
	@ResponseBody
	@RequestMapping(value = "/insertInfo",method = RequestMethod.POST)
	public ApiResult insertInfo(HttpServletRequest request,String type, Integer easyArId){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(!"share".equals(type) || !"give".equals(type)
				&& !"uploadImage".equals(type) || !"uploadVideo".equals(type)){
			if("share".equals(type) || "give".equals(type)){
				if(admin != null && !"".equals(admin)){
					if(type.equals("give")){
						String where = " and type = 'give' and easy_ar_id = " + easyArId + " and admin_id = " + admin.getId();
						List<Infomation> list = infomationService.selectByWhere(where);
						if(list.size() > 0){
							infomationService.deleteByWhere(where);
							result.msg.status = -1;
							result.msg.desc = "取消成功";
							return result;
						}
					}
				}else{
					result.msg.status = 2;
					result.msg.desc = "当前登录已失效，请重新登录";
					return result;
				}
			}
			int i = insertInfo(type, easyArId, admin);
			if(i > 0){
				result.msg.status = 0;
				result.msg.desc = "操作成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "系统错误，操作失败";
			}
		}else{
			result.msg.status = 3;
			result.msg.desc = "类型不正常，请重新输入";
		}
		return result;
	}

	
	/**
	 * 删除我制作的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/delMyMake")
	public ApiResult delMyMake(Long easyArId,HttpServletRequest request){
		ApiResult result = new ApiResult();
		
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			if(easyArId != 0){
				EasyAr ea = easyArService.selectByPrimaryKey(easyArId);
				if(ea != null && !"".equals(ea)){
					try {
						int i = easyArService.deleted(easyArId,admin);
						if(i > 0 ){
							result.msg.status = 0;
							result.msg.desc = "删除成功";
						}else{
							result.msg.status = 1;
							result.msg.desc = "系统错误，删除失败";
						}
					} catch (Exception e) {
						result.msg.status = 1;
						result.msg.desc = "系统错误，删除失败";
					}
				}else{
					result.msg.status = 0;
					result.msg.desc = "删除成功";
				}
			} else {
				result.msg.status = 3;
				result.msg.desc = "参数不能为空";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
	

	private int insertInfo(String type, Integer easyArId, Admin admin) {
		Infomation info = new Infomation();
		if(admin != null && !"".equals(admin)){
			info.setAdminId(admin.getId());
		}
		info.setType(type);
		info.setEasyArId(easyArId);
		info.setCreatedAt(new Date());
		return infomationService.insert(info);
	}

	
	/**
	 * 我分享的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyShare")
	public ApiResult getMyShare(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = " and id in (select easy_ar_id from infomation where admin_id = " + admin.getId() + " and type = 'share')";
			where += " and  status = 'on'";
			List<EasyAr> list = easyArService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无分享作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}

	
	/**
	 * 我制作的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyMake")
	public ApiResult getMyMake(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = " and admin_id = " + admin.getId();
			where += " and  status = 'on'";
			List<EasyAr> list = easyArService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无制作作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}

	
	/**
	 * 我赞过的
	 * 作者： 张叶
	 * 创建时间：2018年6月25日  上午9:00:09
	 */
	@ResponseBody
	@RequestMapping("/getMyGive")
	public ApiResult getMyGive(HttpServletRequest request){
		ApiResult result = new ApiResult();
		Admin admin = safeSessionService.getCurrent(request);
		if(admin != null){
			String where = " and id in (select easy_ar_id from infomation where admin_id = " + admin.getId() + " and type = 'give')";
			List<EasyAr> list = easyArService.selectByWhere(where);
			if(list.size() > 0){
				result.data.put("ar", list);
				result.msg.status = 0;
				result.msg.desc = "获取成功";
			}else{
				result.msg.status = 1;
				result.msg.desc = "暂无点赞作品";
			}
		} else {
			result.msg.status = 2;
			result.msg.desc = "当前登录已失效，请重新登录";
		}
		return result;
	}
}
